package ProcessingAudioWindows;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import ModelApplication.Filter;

public class W2AudioProcessingWindow extends AudioProcessingWindow {
    private JTextField textField;
    private JTable table;
    JRadioButton quarterRadioButton, halfRadioButton, noneRadioButton;

    /**
     * Create the application.
     * @wbp.parser.constructor
     */
    public W2AudioProcessingWindow() {
        super();
        initialize();
    }
    
    public W2AudioProcessingWindow(int x, int y) {
        super(x, y);
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {   
        frame = new JFrame();
        frame.setBounds( 100, 100, 400, 400 );
        
        JPanel panel_1 = new JPanel();
        frame.getContentPane().add(panel_1, BorderLayout.NORTH);
        panel_1.setLayout(new BorderLayout(0, 0));
        
        JPanel panel = new JPanel();
        panel_1.add(panel, BorderLayout.NORTH);
        
        JLabel lblNakadanieOkna = new JLabel("Nakładanie okna");
        panel.add(lblNakadanieOkna);
        
        
        noneRadioButton = new JRadioButton("brak");
        panel.add(noneRadioButton);
        
        halfRadioButton = new JRadioButton("1/2");
        halfRadioButton.setSelected(true);
        panel.add(halfRadioButton);
        
        quarterRadioButton = new JRadioButton("3/4");
        panel.add(quarterRadioButton);
        
        ButtonGroup bg = new ButtonGroup();
        bg.add( noneRadioButton );
        bg.add( halfRadioButton );
        bg.add( quarterRadioButton );
        
        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);
        
        JLabel lblSzerokoOkna = new JLabel("Szerokość okna: ");
        panel_2.add(lblSzerokoOkna);
        
        textField = new JTextField();
        textField.setText("4096");
        panel_2.add(textField);
        textField.setColumns(10);
        
        JPanel panel_3 = new JPanel();
        frame.getContentPane().add(panel_3, BorderLayout.CENTER);
        panel_3.setLayout(new BorderLayout(0, 0));
        
        JLabel lblFiltry = new JLabel("Filtry pasmowoprzepustowe");
        panel_3.add(lblFiltry, BorderLayout.NORTH);
        
        table = new JTable();
        table.setModel(new DefaultTableModel(
            new Object[][] {
                {"86", "172"},
                {"172", "345"},
                {"345", "700"},
                {"700", "1400"},
                {"1400", "2800"},
                {"2800", "5500"},
                {"5500", "20000"},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
            },
            new String[] {
                "Zakres dolny", "Zakres g\u00F3rny"
            }
        ));
        panel_3.add(new JScrollPane(table), BorderLayout.WEST);
        //.add(table, BorderLayout.WEST);
        
        JButton btnPrzywr = new JButton("Przywróć");
        btnPrzywr.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                table.setModel(new DefaultTableModel(
                        new Object[][] {
                            {"86", "172"},
                            {"172", "345"},
                            {"345", "700"},
                            {"700", "1400"},
                            {"1400", "2800"},
                            {"2800", "5500"},
                            {"5500", "20000"},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                            {null, null},
                        },
                        new String[] {
                                "Zakres dolny", "Zakres g\u00F3rny"
                        }
                    ));
                
                textField.setText("4096");
                halfRadioButton.setSelected(true);
                
            }
        });
        
        JScrollPane scrollPane = new JScrollPane();
        panel_3.add(scrollPane, BorderLayout.EAST);
        panel_3.add(btnPrzywr, BorderLayout.SOUTH);
        
        JPanel panel_4 = new JPanel();
        frame.getContentPane().add(panel_4, BorderLayout.SOUTH);
        panel_4.setLayout(new BorderLayout(0, 0));
        
        JButton btnOk = new JButton("Ok");
        panel_4.add(btnOk);
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame.setVisible( false );
            }
        });
        
        frame.setLocation( x, y );
    }
    
    public int getWindowWidth() {
        return Integer.parseInt( textField.getText() );
    }
    
    public int getWindowOverlap() {
        if(noneRadioButton.isSelected()) {
            return 0;
        } else if(halfRadioButton.isSelected()) {
            return 1;
        }
        return 2;
    }
    
    public List<Filter> getFilters() {
        int i;
        for(i = 0; i < table.getRowCount(); i++) {
            if(table.getModel().getValueAt( i, 0 ) == null || table.getModel().getValueAt( i, 0 ).toString().isEmpty()) {
                break;
            }
        }
        
        List<Filter> filters = new ArrayList<Filter>();
        for(int w = 0; w < i; w++) {
            filters.add(new Filter(Integer.parseInt( table.getModel().getValueAt( w, 0 ).toString() ), Integer.parseInt( table.getModel().getValueAt( w, 1 ).toString() )));
        }
        
        return filters;
    }
    
}
